package iterables;
import java.util.*;

	public class IterablesAlternados<T> implements Iterable<T>{
		  private Iterable <T> it1;
		  private Iterable <T> it2;

		  public IterablesAlternados(Iterable<T> i1, Iterable<T> i2){
		 	 it1=i1;
		   	 it2=i2;
		  }	  
		  public Iterator<T> iterator(){
		 	 return new IteradorAlternado(it1.iterator(),it2.iterator());
		  }
		  private class IteradorAlternado implements Iterator<T>{
			  private Iterator <T> it1;
			  private Iterator <T> it2;
			  private boolean l=true; 
			  public IteradorAlternado(Iterator <T> i1, Iterator <T> i2) {
			   it1 = i1;
			   it2 = i2;
			   }
			   public boolean hasNext() {
			      return it1.hasNext()||it2.hasNext();
			   }
			   public T next() {
				   T s = null;
				   if(it1.hasNext()&& it2.hasNext()){
				     if(l){
				       s = it1.next();
				     }else{
				       s = it2.next();
				     }
				   l = !l;
				   }else {
				     if(it1.hasNext()){
				       s = it1.next();
				     }
				   if(it2.hasNext()){
				     s = it2.next();
				   } 
				 }
				 return s;
				 }

			   public void remove() {
			   }
			}


}
